package com.huifaapp.mall.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.R2;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.InvitationVIP;
import com.huifaapp.mall.bean.InviteCollarNow;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Contig;
import com.huifaapp.mall.uitils.DownImage;
import com.huifaapp.mall.uitils.ImgUtils;
import com.huifaapp.mall.uitils.UtilsImage;
import com.huifaapp.mall.view.InviteCollarWin;
import com.squareup.okhttp.Request;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2018/1/22.
 */

public class InvitationVIPActivity extends AppCompatActivity implements View.OnClickListener,InviteCollarWin.OnItemClickListener{

    private ImageView iv_invitetion_vip_back;
    private TextView tv_invitation_vip_record;
    private TextView tv_invitation_vip_lijiyaoqing;
    private TextView tv_invitation_vip_mianduimianyaoqing;
    InviteCollarWin mPop;
    InvitationVIP list;
    private String qrcode;
    private LinearLayout ll_mianduimian;
    private LinearLayout main_view_invite_vip;
    private ImageView iv_invitation_vip_guanbi;
    private ImageView iv_invitation_vip_erweima;
    private TextView tv_weixinhaoyou_vip,tv_pengyouquan_vip,tv_qqhaoyou_vip,tv_xinlangweibo_vip,tv_baocunshouji_vip;
    private TextView tv_xxsendyouhuiquan;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.invitation_vip_activity);
        iv_invitetion_vip_back= (ImageView) findViewById(R.id.iv_invitetion_vip_back);
        iv_invitetion_vip_back.setOnClickListener(this);
        tv_invitation_vip_record= (TextView) findViewById(R.id.tv_invitation_vip_record);
        tv_invitation_vip_record.setOnClickListener(this);
        tv_invitation_vip_lijiyaoqing= (TextView) findViewById(R.id.tv_invitation_vip_lijiyaoqing);
        tv_invitation_vip_lijiyaoqing.setOnClickListener(this);
        tv_invitation_vip_mianduimianyaoqing= (TextView) findViewById(R.id.tv_invitation_vip_mianduimianyaoqing);
        tv_invitation_vip_mianduimianyaoqing.setOnClickListener(this);
        ll_mianduimian= (LinearLayout) findViewById(R.id.ll_mianduimian);
        main_view_invite_vip= (LinearLayout) findViewById(R.id.main_view_invite_vip);
        iv_invitation_vip_guanbi= (ImageView) findViewById(R.id.iv_invitation_vip_guanbi);
        iv_invitation_vip_guanbi.setOnClickListener(this);
        iv_invitation_vip_erweima= (ImageView) findViewById(R.id.iv_invitation_vip_erweima);
        tv_weixinhaoyou_vip= (TextView) findViewById(R.id.tv_weixinhaoyou_vip);
        tv_weixinhaoyou_vip.setOnClickListener(this);
        tv_pengyouquan_vip= (TextView) findViewById(R.id.tv_pengyouquan_vip);
        tv_pengyouquan_vip.setOnClickListener(this);
        tv_qqhaoyou_vip= (TextView) findViewById(R.id.tv_qqhaoyou_vip);
        tv_qqhaoyou_vip.setOnClickListener(this);
        tv_xinlangweibo_vip= (TextView) findViewById(R.id.tv_xinlangweibo_vip);
        tv_xinlangweibo_vip.setOnClickListener(this);
        tv_baocunshouji_vip= (TextView) findViewById(R.id.tv_baocunshouji_vip);
        tv_baocunshouji_vip.setOnClickListener(this);
        tv_xxsendyouhuiquan= (TextView) findViewById(R.id.tv_xxsendyouhuiquan);
        Invitelaod();
        ll_mianduimian.setVisibility(View.GONE);
        main_view_invite_vip.setVisibility(View.VISIBLE);
        mPop=new InviteCollarWin(this);
        mPop.setOnItemClickListener(this);
        mPop.setFocusable(false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_invitetion_vip_back:
                finish();
                break;
            case R.id.tv_invitation_vip_record:
                Intent intent=new Intent(InvitationVIPActivity.this,InvitationVIPRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_invitation_vip_lijiyaoqing:
                Invitelaod();
                //设置PopupWindow中的位置T4
                mPop.showAtLocation(InvitationVIPActivity.this.findViewById(R.id.main_view_invite_vip), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.tv_invitation_vip_mianduimianyaoqing:
                ll_mianduimian.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_invitation_vip_guanbi:
                ll_mianduimian.setVisibility(View.GONE);
                break;
            case R.id.tv_weixinhaoyou_vip:
//                UMImage image = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
//                shareUrl("1",null, null,image);
                final UMWeb web = new UMWeb(list.getData().getLink().getUrl());
                web.setTitle(list.getData().getLink().getTitle());//标题
                web.setThumb(new UMImage(InvitationVIPActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "1",null,web,null);
                break;
            case R.id.tv_pengyouquan_vip:
                final UMWeb web2 = new UMWeb(list.getData().getLink().getUrl());
                web2.setTitle(list.getData().getLink().getTitle());//标题
                web2.setThumb(new UMImage(InvitationVIPActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web2.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "2",null,web2,null);
//                UMImage image1 = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
//                shareUrl("2",null, null,image1);
                break;
            case R.id.tv_qqhaoyou_vip:
                final UMWeb web3 = new UMWeb(list.getData().getLink().getUrl());
                web3.setTitle(list.getData().getLink().getTitle());//标题
                web3.setThumb(new UMImage(InvitationVIPActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web3.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "3",null,web3,null);
//                UMImage imageqq = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
//                shareUrl("3",null, null,imageqq);
                break;
            case R.id.tv_xinlangweibo_vip:
                final UMWeb web4 = new UMWeb(list.getData().getLink().getUrl());
                web4.setTitle(list.getData().getLink().getTitle());//标题
                web4.setThumb(new UMImage(InvitationVIPActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web4.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "4",null,web4,null);
//                UMImage imagesl = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
//                shareUrl("4",null, null,imagesl);
                break;
            case R.id.tv_baocunshouji_vip:
                try {
                    new DownImage().execute(list.getData().getInvitingImg());
                    Log.e("SHUJU",Contig.bitmap+"            ");
                    if(Contig.bitmap.equals(null)||Contig.bitmap.equals("null")||Contig.bitmap.equals("")) {
                        new DownImage().execute(list.getData().getInvitingImg());
                        Toast.makeText(InvitationVIPActivity.this,"您未点击到保存手机，请重新点击保存到手机!",Toast.LENGTH_SHORT).show();
                    }else {
                        ImgUtils.saveImageToGallery(InvitationVIPActivity.this, Contig.bitmap);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

//              ImgUtils.saveImageToGallery(InvitationVIPActivity.this,bitmap);
                break;
        }
    }
    private void Invitelaod(){
        Log.e("邀请","url=============="+ ApiManager.USER_INVITE_VIP+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(InvitationVIPActivity.this));
        OkHttpUtils.get().url(ApiManager.USER_INVITE_VIP+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(InvitationVIPActivity.this))
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        list = gson.fromJson(response,InvitationVIP.class);
                        if(list.getCode().equals("10000")){
                            qrcode = list.getData().getQrcode();
                            UtilsImage.uploadimageview(iv_invitation_vip_erweima,qrcode,"",InvitationVIPActivity.this);
                            tv_xxsendyouhuiquan.setText(list.getData().getTipword());
                        }else if(list.getCode().equals("10001")){
                            Intent intent=new Intent(InvitationVIPActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();

                        }else if(list.getCode().equals("10002")){
                            Intent intent=new Intent(InvitationVIPActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }else{
                            Toast.makeText(InvitationVIPActivity.this,list.getMsg(),Toast.LENGTH_SHORT).show();
                        }

                    }
           });
    }


    @Override
    public void setOnItemClick(View v) {
        switch (v.getId()){
            case R.id.tv_shengchenghaibao:
//                Intent intent=new Intent(InvitationVIPActivity.this,VIPPosterActivity.class);
//                intent.putExtra("qrcode",qrcode);
//                intent.putExtra("invitingimg",list.getData().getInvitingImg());
//                startActivity(intent);
                ll_mianduimian.setVisibility(View.VISIBLE);
                mPop.dismiss();
                break;
            case R.id.tv_weixinhaoyou:
                UMImage image = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("1",null, null,image);
                break;
            case R.id.tv_pengyouquan:
                UMImage image1 = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("2",null, null,image1);
                break;
            case R.id.tv_qqhaoyou:
                UMImage imageqq = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("3",null, null,imageqq);
                break;
            case R.id.tv_xinlangweibo:
                UMImage imagesl = new UMImage(InvitationVIPActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("4",null, null,imagesl);
                break;
            case R.id.tv_copylianjie:
                ClipboardUtils.copyText(list.getData().getInvitingImg());
                Toast.makeText(InvitationVIPActivity.this,"复制成功!",Toast.LENGTH_SHORT).show();
                break;

        }
    }
        /**
         * 分享链接
         */

    private void shareUrl(String type, String con, UMWeb web, UMImage image) {

        android.util.Log.v("shiran","-----------shareUrl()----------");


        UMShareListener umShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
//                if (dialog!=null&&!dialog.isShowing()) {
//                    dialog.show();
//                }
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享成功");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享成功");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享成功");
                } else if(share_media ==SHARE_MEDIA.SINA){
                    ToastUtils.showShortSafe("新浪微博分享成功");
                }else {
                    ToastUtils.showShortSafe("分享成功");

                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享失败");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享失败");
                }else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享失败");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享失败");
                } else {
                    ToastUtils.showShortSafe("分享失败");

                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享取消");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享取消");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享取消");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享取消");
                } else {
                    ToastUtils.showShortSafe("分享取消");

                }
            }
        };

        if ("1".equals(type)) {  //分享微信
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.WEIXIN).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();

        }else  if ("2".equals(type)) { //分享朋友圈

            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();

        }else if("3".equals(type)){//分享qq
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.QQ).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        }else if("4".equals(type)){//分享微博
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.SINA).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}


